using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameLib.GameScreen
{
    class LoadingScreen : GameScreen
    {
        SpriteFont font;
        String loadingMessage;
        String progressMessage;
        Vector2 loadingMessagePosition;
        Vector2 progressMessagePosition;
        static GameScreen newScreen;

        int LastChange = 0;
        int Remaining = 1500;

        public LoadingScreen(ScreenManager man)
            : base(man)
        {
            Load();
        }

        public static void LoadScreen(ScreenManager manager, ScreenName scr)
        {
            LoadingScreen loading = new LoadingScreen(manager);
            manager.AddScreen(loading);

            switch (scr)
            {
                case ScreenName.GamePlay:
                    newScreen = new GamePlayScreen(manager);
                    break;

                case ScreenName.GameLevel:
                    newScreen = new GameLevelScreen(manager);
                    break;

                case ScreenName.GameTitle:
                    newScreen = new GameTitleScreen(manager);
                    break;
            }
        }

        public override void Load()
        {
            font = content.Load<SpriteFont>(@"Fonts\LoadingFont");
            loadingMessage = "Loading";
            progressMessage = ".";
            Viewport viewport = manager.GraphicsDevice.Viewport;
            Vector2 viewportSize = new Vector2(viewport.Width, viewport.Height);
            Vector2 textSize = font.MeasureString(loadingMessage);
            loadingMessagePosition = (viewportSize - textSize) / 2;

            textSize = font.MeasureString("....");
            progressMessagePosition.X = (viewportSize.X - textSize.X) / 2;
            progressMessagePosition.Y = loadingMessagePosition.Y + 20;
        }

        public override void UnLoad()
        {
            base.UnLoad();
        }

        public override void Update(GameTime gametime)
        {
            Remaining -= gametime.ElapsedGameTime.Milliseconds;
            LastChange += gametime.ElapsedGameTime.Milliseconds;
            if (LastChange >= 500)
            {
                LastChange = 0;
                switch (progressMessage.Length)
                {
                    case 1:
                        progressMessage = "..";
                        break;
                    case 2:
                        progressMessage = "...";
                        break;
                    case 3:
                        progressMessage = "....";
                        break;
                    case 4:
                        progressMessage = ".";
                        break;
                }
            }

            if (Remaining <= 0 && newScreen.LoadComplete)
            {
                newScreen.FadeIn();
                manager.AddScreen(newScreen);
                manager.RemoveScreen(this);
            }
            base.Update(gametime);
        }

        public override void Draw(SpriteBatch spritebatch)
        {
            spritebatch.DrawString(font, loadingMessage, loadingMessagePosition, Color.White);
            spritebatch.DrawString(font, progressMessage, progressMessagePosition, Color.White);
        }
    }
}
